<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>shell 中的特殊符号 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>shell 中的特殊符号</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell/">shell</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;shell中常用到的特殊字符。</p>
<ul>
<li>* 代表零个或多个任意字符</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost 111]<span class="comment"># ls</span></div><div class="line">1 11.txt 1.txt 22.txt 2.txt 33.txt 3.txt 44.zip 55.zip</div><div class="line">[root@localhost 111]<span class="comment"># ls *.txt</span></div><div class="line">11.txt 1.txt 22.txt 2.txt 33.txt 3.txt</div></pre></td></tr></table></figure>
<ul>
<li>？ 只代表一个任意的字符</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost 111]<span class="comment"># ls</span></div><div class="line">1 11.txt 1.txt 22.txt 2.txt 33.txt 3.txt 44.zip 55.zip</div><div class="line">[root@localhost 111]<span class="comment"># ls ?.txt</span></div><div class="line">1.txt 2.txt 3.txt</div></pre></td></tr></table></figure>
<ul>
<li># 注释符号</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个符号在linux中表示注释说明的意思，即#后面的内容linux忽略掉。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># abc=123 #aaa</span></div><div class="line">[root@localhost ~]<span class="comment"># echo $abc</span></div><div class="line">123</div></pre></td></tr></table></figure>
<ul>
<li>\ 脱意字符<br>  它将后面的特殊符号（例如“*”）还原为普通字符。</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ls -d test\*</span></div><div class="line">ls: 无法访问<span class="built_in">test</span>*: 没有那个文件或目录</div></pre></td></tr></table></figure>
<ul>
<li>| 管道符</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;它的作用在于将符号前面命令的结果丢给符号后面的命令。这里提到的后面命令，并不是所有的命令都可以的，一般针对文档操作的命令比较常用，例如 cat , less , head , tail , greo , cut , sort , wc , unip , tee , tr , split , sed , awk 等等，其中grep ，sed ，awk 为正则表达式。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cat /etc/passwd|wc -l</span></div><div class="line">21</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;wc -l用来计算一个文档有多少行。</p>
<ul>
<li>特殊符号 $</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;$ 除了用于变量前面的标识符外，还有一个妙用，就是和“!”结合起来使用。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ls 1.py</span></div><div class="line">1.py</div><div class="line">[root@localhost ~]<span class="comment"># ls !$</span></div><div class="line">ls 1.py</div><div class="line">1.py</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;!$ 表示上条命令中最后一个变量（总之就是上条命令中最后出现的那个东西）例如上边命令最后是 1.py 那么在当前命令下输入 !$ 则代表 1.py</p>
<ul>
<li>特殊符号 ；</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;平时在一行中敲一个命令，然后回车就运行了，那么在一行中运行两个或两个以上的命令则需要在命令之间加一个“；”</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ls *.py;touch 4.py;ls *.py</span></div><div class="line">1.py  2.py  3.py</div><div class="line">1.py  2.py  3.py  4.py</div></pre></td></tr></table></figure>
<ul>
<li>特殊符号 ~</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用户的家目录。如果是root则是/root。普通用户则是/home/username</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cd ~</span></div><div class="line">[root@localhost ~]<span class="comment"># pwd</span></div><div class="line">/root</div><div class="line">[root@localhost ~]<span class="comment"># su test</span></div><div class="line">[<span class="built_in">test</span>@localhost root]$ <span class="built_in">cd</span> ~</div><div class="line">[<span class="built_in">test</span>@localhost ~]<span class="comment"># pwd</span></div><div class="line">/home/<span class="built_in">test</span></div></pre></td></tr></table></figure>
<ul>
<li>特殊符号 &amp;</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果想把一条命令放到后台执行的话，则需要加上“&amp;”这个符号。通常用于时间非常长的情况。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># sleep 100 &amp;</span></div><div class="line">[2] 2393</div><div class="line">[root@localhost ~]<span class="comment"># jobs</span></div><div class="line">[1]+  运行中               sleep 100 &amp;</div></pre></td></tr></table></figure>
<ul>
<li>重定向符号 &gt; &gt;&gt; 2&gt; 2&gt;&gt;</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重定向符号&gt;以及&gt;&gt;,分别表示取代和追加的意思。然后还有两个符号就是2&gt;和2&gt;&gt;，分别表示错误重定向和错误追加重定向。当运行一个命令报错时，报错信息会输出到当前的屏幕，如果想重定向到一个文本里，则要用2&gt;或者2&gt;&gt;。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ls aaaa</span></div><div class="line">ls: 无法访问aaaa: 没有那个文件或目录</div><div class="line">[root@localhost ~]<span class="comment"># ls aaaa 2&gt; /tmp/error</span></div><div class="line">[root@localhost ~]<span class="comment"># cat /tmp/error</span></div><div class="line">ls: 无法访问aaaa: 没有那个文件或目录</div><div class="line">[root@localhost ~]<span class="comment"># ls aaaa 2&gt;&gt; /tmp/errror</span></div><div class="line">[root@localhost ~]<span class="comment"># cat /tmp/error</span></div><div class="line">ls: 无法访问aaaa: 没有那个文件或目录</div></pre></td></tr></table></figure>
<ul>
<li>中括号 []</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;中间为字符组合，代表中间字符中的任意一个。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ls *.py</span></div><div class="line">1.py  2.py  3.py  4.py</div><div class="line">[root@localhost ~]<span class="comment"># ls [1-3].py</span></div><div class="line">1.py  2.py  3.py</div><div class="line">[root@localhost ~]<span class="comment"># ls [0-9].py</span></div><div class="line">1.py  2.py  3.py  4.py</div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/Shell/3. shell 中的特殊符号/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
